<template>
  <div class="app-container">
    <el-button
      size="mini"
      type="primary"
      icon="el-icon-edit"
      @click="takeScreenshot('screenshot-area')"
      >截图</el-button
    >
    <div v-if="screenshot">
      <img :src="screenshot" style="width: 100%" />
    </div>
    <el-row :gutter="10" class="mb8">
      <right-toolbar @queryTable="refresh"></right-toolbar>
    </el-row>
    <div id="screenshot-area">
      <el-container>
        <el-descriptions
          class="margin-top"
          v-bind:title="info.date"
          direction="vertical"
          :column="4"
          border
        >
          <el-descriptions-item label="登记人数">
            {{ info.dayData.登记人数 }}
          </el-descriptions-item>
          <el-descriptions-item label="固定献血浆者">
            {{ info.dayData.固定浆员数量 }}
          </el-descriptions-item>
          <el-descriptions-item label="新献血浆者">
            {{ info.dayData.新浆员数量 }}
          </el-descriptions-item>
          <el-descriptions-item label="非固定献血浆者">
            {{ info.dayData.非固定浆员数量 }}
          </el-descriptions-item>
          <el-descriptions-item label="采浆量">
            {{ info.dayData.采浆量 }}
          </el-descriptions-item>
          <el-descriptions-item label="上轮采浆量">
            {{ info.dayData.上轮采浆量 }}
          </el-descriptions-item>
          <el-descriptions-item label="较上轮增减">
            {{ info.dayData.采浆量 - info.dayData.上轮采浆量 }}
          </el-descriptions-item>
          <el-descriptions-item label="未采浆人数">
            {{ info.dayData.未采浆人数 }}
          </el-descriptions-item>
          <el-descriptions-item label="当日首次采浆数">
            {{ indicators.dayData.当日首次采浆数 }}
          </el-descriptions-item>
          <el-descriptions-item label="当日建档数">
            {{ indicators.dayData.当日建档数 }}
          </el-descriptions-item>
          <el-descriptions-item label="本月建档当日献浆数">
            {{ indicators.dayData.本月建档当日献浆数 }}
          </el-descriptions-item>
        </el-descriptions>
        <div id="unCollectionTable">
          <el-table
            v-loading="unCollectionLoading"
            :data="data.resultList"
            :row-class-name="tableRowClassName"
            style="width: 100%"
            size="mini"
          >
            <el-table-column
              label="序号"
              width="60"
              align="center"
              header-align="center"
            >
              <template slot-scope="scope">
                {{ scope.$index + 1 }}
              </template>
            </el-table-column>
            <el-table-column
              prop="processno"
              label="流水号"
              width="70"
              align="center"
              header-align="center"
            >
              <template slot-scope="scope">
                {{ scope.row.processno }}
              </template>
            </el-table-column>
            <el-table-column
              prop="processno"
              label="未采时长"
              width="80"
              align="center"
              header-align="center"
            >
              <template slot-scope="scope">
                <el-tag
                  v-if="scope.row.intervalTime >= 2"
                  effect="dark"
                  type="danger"
                  disable-transitions
                >
                  {{ scope.row.intervalTime }}
                </el-tag>
                <span v-if="scope.row.intervalTime < 2">
                  {{ scope.row.intervalTime }}
                </span>
              </template>
            </el-table-column>
            <el-table-column
              prop="donorname"
              label="姓名"
              show-overflow-tooltip
              width="80"
              align="center"
              header-align="center"
            >
              <template slot-scope="scope">
                {{ scope.row.donorname }}
              </template>
            </el-table-column>
            <el-table-column
              prop="phone"
              label="联系方式"
              width="120"
              align="center"
              header-align="center"
            >
              <template slot-scope="scope">
                <el-button size="mini" @click="callPhone(scope.row.phone)">
                  {{ scope.row.phone }}</el-button
                >
              </template>
            </el-table-column>
            <el-table-column
              prop="reason"
              label="回访结果"
              align="center"
              header-align="center"
            >
              <template slot-scope="scope">
                <el-input
                  v-model="scope.row.reason"
                  placeholder="输入回访结果"
                  @blur="saveUnCollection(scope.$index, scope.row)"
                />
              </template>
            </el-table-column>
            <el-table-column
              prop="type"
              label="未采类型"
              align="center"
              header-align="center"
            >
              <template slot-scope="scope">
                <el-select
                  placeholder="请选择未采类型"
                  v-model="scope.row.type"
                  @change="saveUnCollection(scope.$index, scope.row)"
                  clearable
                >
                  <el-option
                    v-for="dict in dict.type.day_uncollection_type"
                    :key="dict.value"
                    :label="dict.label"
                    :value="dict.value"
                  />
                </el-select>
              </template>
            </el-table-column>
            <el-table-column
              prop="changeless"
              label="浆员类型"
              width="80"
              align="center"
              header-align="center"
            >
              <template slot-scope="scope">
                {{ scope.row.changeless == "NEW" ? "新" : "" }}
                {{ scope.row.changeless == "N" ? "非固定" : "" }}
                {{ scope.row.changeless == "Y" ? "固定" : "" }}
              </template>
            </el-table-column>
            <el-table-column
              prop="codedonorstate"
              label="浆员状态"
              width="80"
              align="center"
              header-align="center"
            >
              <template slot-scope="scope">
                <el-tag
                  effect="dark"
                  :type="
                    scope.row.codedonorstate === 'YJ' ||
                    scope.row.codedonorstate === 'ZJ'
                      ? 'danger'
                      : 'success'
                  "
                  disable-transitions
                >
                  {{ scope.row.codedonorstate == "YJ" ? "永拒" : "" }}
                  {{ scope.row.codedonorstate == "ZJ" ? "暂拒" : "" }}
                  {{ scope.row.codedonorstate == "N" ? "正常" : "" }}
                </el-tag>
              </template>
            </el-table-column>
            <el-table-column
              prop="cancelreason"
              label="拒绝原因"
              align="center"
              header-align="center"
            >
              <template slot-scope="scope">
                {{ scope.row.cancelreason }}
              </template>
            </el-table-column>
            <el-table-column
              prop="plasmacheck"
              label="采前核查"
              width="50"
              align="center"
              header-align="center"
            >
              <template slot-scope="scope">
                <el-tag
                  effect="dark"
                  :type="scope.row.plasmacheck === 'N' ? 'danger' : 'success'"
                  disable-transitions
                  >{{ scope.row.plasmacheck }}</el-tag
                >
              </template>
            </el-table-column>
            <el-table-column
              prop="plasmacollection"
              label="脂浆"
              width="50"
              align="center"
              header-align="center"
            >
              <template slot-scope="scope">
                <el-tag
                  v-if="scope.row.plasmacollection === 'I'"
                  effect="dark"
                  type="danger"
                  disable-transitions
                >
                  {{ scope.row.plasmacollection == "I" ? "脂浆" : "" }}
                </el-tag>
              </template>
            </el-table-column>
            <el-table-column
              prop="physicalask"
              label="健康征询"
              width="50"
              align="center"
              header-align="center"
            >
              <template slot-scope="scope">
                <el-tag
                  v-if="scope.row.physicalask === 'N'"
                  effect="dark"
                  type="danger"
                  disable-transitions
                  >{{ scope.row.physicalask }}</el-tag
                >
              </template>
            </el-table-column>
            <el-table-column
              prop="physical"
              label="体格检查"
              width="50"
              align="center"
              header-align="center"
            >
              <template slot-scope="scope">
                <el-tag
                  v-if="scope.row.physical === 'N'"
                  effect="dark"
                  type="danger"
                  disable-transitions
                  >{{ scope.row.physical }}</el-tag
                >
              </template>
            </el-table-column>
            <el-table-column
              prop="physicalyear"
              label="胸片录入"
              width="50"
              align="center"
              header-align="center"
            >
              <template slot-scope="scope">
                <el-tag
                  v-if="scope.row.physicalyear === 'N'"
                  effect="dark"
                  type="danger"
                  disable-transitions
                  >{{ scope.row.physicalyear }}</el-tag
                >
              </template>
            </el-table-column>
            <el-table-column
              prop="physicalecg"
              label="心电图"
              width="50"
              align="center"
              header-align="center"
            >
              <template slot-scope="scope">
                <el-tag
                  v-if="scope.row.physicalecg === 'N'"
                  effect="dark"
                  type="danger"
                  disable-transitions
                  >{{ scope.row.physicalecg }}</el-tag
                >
              </template>
            </el-table-column>
            <el-table-column
              prop="bloodtest"
              label="血液检测"
              width="50"
              align="center"
              header-align="center"
            >
              <template slot-scope="scope">
                <el-tag
                  v-if="scope.row.bloodtest === 'N'"
                  effect="dark"
                  type="danger"
                  disable-transitions
                  >{{ scope.row.bloodtest }}</el-tag
                >
              </template>
            </el-table-column>
            <el-table-column
              prop="bloodsamplecollect"
              label="标本采集"
              width="50"
              align="center"
              header-align="center"
            >
              <template slot-scope="scope">
                <el-tag
                  v-if="scope.row.bloodsamplecollect === 'N'"
                  effect="dark"
                  type="danger"
                  disable-transitions
                  >{{ scope.row.bloodsamplecollect }}</el-tag
                >
              </template>
            </el-table-column>
            <el-table-column
              prop="hbBloodtest"
              label="血红蛋白"
              width="50"
              align="center"
              header-align="center"
            >
              <template slot-scope="scope">
                <el-tag
                  v-if="scope.row.hbBloodtest === 'N'"
                  effect="dark"
                  type="danger"
                  disable-transitions
                  >{{ scope.row.hbBloodtest }}</el-tag
                >
              </template>
            </el-table-column>
          </el-table>
        </div>
        <el-footer></el-footer>
      </el-container>
      <el-container>
        <el-header class="header"> </el-header>
        <el-main>
          <el-table
            v-loading="dayreportLoading"
            :data="this.dayReportList"
            style="width: 100%"
          >
            <el-table-column prop="当天日期" label="日期" min-width="70">
              <template slot-scope="scope">
                {{ scope.row.当天日期.slice(5, 11) }}
              </template>
            </el-table-column>
            <el-table-column>
              <template slot="header">
                <div>采浆量</div>
                <div>任务数</div>
              </template>
              <el-table-column prop="当日采浆量" label="当日采浆量">
              </el-table-column>
            </el-table-column>
            <el-table-column v-bind:label="'' + indicators.采浆量">
              <el-table-column prop="本月采浆量" label="已完成袋数">
              </el-table-column>
            </el-table-column>
            <el-table-column>
              <template slot="header">
                <div>新献血浆者</div>
                <div>任务数</div>
              </template>
              <el-table-column label="未完成袋数">
                <template slot-scope="scope">
                  {{ indicators.采浆量 - scope.row.本月采浆量 }}
                </template>
              </el-table-column>
            </el-table-column>
            <el-table-column v-bind:label="'' + indicators.新浆员发展数">
              <el-table-column>
                <template slot="header">
                  <div>本月建档</div>
                  <div>当日献浆数</div>
                </template>
                <template slot-scope="scope">
                  {{ scope.row.本月建档当日献浆数 }}
                </template>
              </el-table-column>
            </el-table-column>
            <el-table-column label="采浆量85%">
              <el-table-column prop="本月建档并献浆数" label="已完成新浆员">
              </el-table-column>
            </el-table-column>
            <el-table-column
              v-bind:label="'' + Math.ceil(indicators.采浆量 * 0.9)"
            >
              <el-table-column label="未完成新浆员">
                <template slot-scope="scope">
                  {{ indicators.新浆员发展数 - scope.row.本月建档并献浆数 }}
                </template>
              </el-table-column>
            </el-table-column>
            <el-table-column label="新献血浆者85%">
              <el-table-column prop="剩余天数" label="剩余天数">
              </el-table-column>
            </el-table-column>
            <el-table-column
              v-bind:label="'' + Math.ceil(indicators.新浆员发展数 * 0.9)"
            >
              <el-table-column label="日均袋数">
                <template slot-scope="scope">
                  {{
                    Math.ceil(
                      (indicators.采浆量 - scope.row.本月采浆量) /
                        scope.row.剩余天数
                    )
                  }}
                </template>
              </el-table-column>
            </el-table-column>
            <el-table-column label="目标留存率">
              <el-table-column label="当前留存率">
                <el-table-column label="日均新人数">
                  <template slot-scope="scope">
                    {{
                      Math.ceil(
                        (indicators.新浆员发展数 - scope.row.本月建档并献浆数) /
                          scope.row.剩余天数
                      )
                    }}
                  </template>
                </el-table-column>
              </el-table-column>
            </el-table-column>
            <el-table-column v-bind:label="'' + indicators.目标留存率">
              <el-table-column v-bind:label="'' + indicators.留存率">
                <el-table-column label="日均袋数">
                  <template slot="header">
                    <div>日均袋数85%</div>
                  </template>
                  <template slot-scope="scope">
                    {{
                      Math.ceil(
                        (indicators.采浆量 * 0.9 - scope.row.本月采浆量) /
                          scope.row.剩余天数
                      )
                    }}
                  </template>
                </el-table-column>
              </el-table-column>
            </el-table-column>
            <el-table-column label="目标频次">
              <el-table-column label="当前频次">
                <el-table-column prop="count" label="日均新人数85%">
                  <template slot="header">
                    <div>日均新人数85%</div>
                  </template>
                  <template slot-scope="scope">
                    {{
                      Math.ceil(
                        (indicators.新浆员发展数 * 0.9 -
                          scope.row.本月建档并献浆数) /
                          scope.row.剩余天数
                      )
                    }}
                  </template>
                </el-table-column>
              </el-table-column>
            </el-table-column>
            <el-table-column v-bind:label="'' + indicators.目标频次">
              <el-table-column v-bind:label="'' + indicators.总频次">
                <el-table-column prop="" label=""> </el-table-column>
              </el-table-column>
            </el-table-column>
          </el-table>
        </el-main>
        <el-footer></el-footer>
      </el-container>
    </div>
  </div>
</template>
  
  <script>
import {
  listUnCollection,
  getUnCollection,
  delUnCollection,
  addUnCollection,
  updateUnCollection,
  saveUnCollection,
} from "@/api/plasma/unCollection";
import {
  plasmaDayDetailReport,
  plasmaDayReport,
  formatDate,
} from "@/api/plasma";
import Vue from "vue";
import html2canvas from "html2canvas";
Vue.prototype.info = {
  date: "",
  dayData: {
    登记人数: "-",
    新浆员数量: "-",
    非固定浆员数量: "-",
    固定浆员数量: "-",
    采浆量: "-",
    上轮采浆量: "-",
    较上轮增减: "-",
    未采浆人数: "-",
  },
};
Vue.prototype.indicators = {
  date: "",
  dayData: {
    当日首次采浆数: "-",
    当日建档数: "-",
    本月建档当日献浆数: "-",
    新浆员发展数: "-",
  },
};

export default {
  name: "UnCollection",
  dicts: ["donor_state", "day_uncollection_type"],
  data() {
    return {
      // 遮罩层
      loading: true,
      unCollectionLoading: true,
      dayreportLoading: true,
      screenshot: null, //用于截图
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 未采人员登记表表格数据
      unCollectionList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        processNo: null,
        donorNo: null,
        reason: null,
        type: null,
        日期: formatDate(new Date(), "yyyy-MM-dd"),
        donorState: null,
        donorType: null,
        donorName: null,
        intervalTime: null,
        phone: null,
        donorId: null,
        followName: null,
        followTime: null,
        plasmaCollection: "Y",
        insertTime: null,
      },
      unCollectionTypeList: [],
      dayReportList: [],
      data: {
        resultList: [],
        dayReportData: {
          新浆员采浆袋数: undefined,
          当年献浆袋数: undefined,
          总频次: undefined,
          当年发展新浆员数: undefined,
          上年参与献浆总人数: undefined,
          新浆员一针率: undefined,
          留存率: undefined,
          当年献浆总人数: undefined,
        },
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {},
    };
  },
  created() {
    this.getPlasmaDayDetailReport();
    this.getPlasmaDayReport();
  },
  methods: {
    /** 查询未采人员登记表列表 */
    refresh() {
      this.getPlasmaDayDetailReport();
      this.getPlasmaDayReport();
    },
    getList() {
      this.loading = true;
      listUnCollection(this.queryParams).then((response) => {
        this.unCollectionList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    getPlasmaDayReport() {
      this.dayreportLoading = true;
      plasmaDayReport().then((res) => {
        this.dayReportList = res.data.dayReportList;
        Vue.prototype.indicators = res.data;
        this.data.dayReportData = res.data;
        this.dayreportLoading = false;
        console.log(res);
      });
    },
    saveUnCollection(index, formData) {
      //formData.type = this.unCollectionTypeList[index]

      console.log(formData);
      saveUnCollection(formData).then((response) => {
        this.$modal.msgSuccess("保存成功");
        this.open = false;
        this.getList();
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        processNo: null,
        donorNo: null,
        reason: null,
        type: null,
        日期: formatDate(new Date(), "yyyy-MM-dd"),
        donorState: null,
        donorType: null,
        donorName: null,
        intervalTime: null,
        phone: null,
        donorId: null,
        followName: null,
        followTime: null,
        plasmaCollection: null,
        insertTime: null,
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate((valid) => {
        if (valid) {
          if (this.form.id != null) {
            updateUnCollection(this.form).then((response) => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addUnCollection(this.form).then((response) => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      this.$modal
        .confirm('是否确认删除未采人员登记表编号为"' + ids + '"的数据项？')
        .then(function () {
          return delUnCollection(ids);
        })
        .then(() => {
          this.getList();
          this.$modal.msgSuccess("删除成功");
        })
        .catch(() => {});
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download(
        "plasma/unCollection/export",
        {
          ...this.queryParams,
        },
        `unCollection_${new Date().getTime()}.xlsx`
      );
    },
    getPlasmaDayDetailReport() {
      this.unCollectionLoading = true;
      plasmaDayDetailReport().then((res) => {
        this.data = res.data;
        console.log(this.data);
        Vue.prototype.info = res.data;
        Vue.prototype.info.dayData.未采浆人数 = this.data.resultList.length;
        this.unCollectionLoading = false;
      });
    },
    tableRowClassName({ row, rowIndex }) {
      if (true) return;
      if (row.codeDonorState == "YJ") {
        return "yj-row";
      } else if (row.codeDonorState == "ZJ") {
        return "zj-row";
      }
      return "";
    },
    callPhone(phoneNum) {
      window.location.href = "tel://" + phoneNum;
    },
    async takeScreenshot(id) {
      const element = document.getElementById(id);
      const width = window.innerWidth;
      const height = window.innerHeight;
      console.log(width, height);
      html2canvas(element, {
        scale: 2, // 设置scale为2，以确保高分辨率的输出，根据需求可以调整
        width: window.innerWidth, // 设置截图的宽度，这里设置为整个窗口的宽度
        height: window.innerHeight, // 设置截图的高度，这里设置为整个窗口的高度
        useCORS: true, // 如果需要抓取跨域图片，设置为true
      }).then((canvas) => {
        // 创建一个图像元素
        let img = new Image();
        img.src = canvas.toDataURL("image/png");
        img.style.width = "100%";
        img.style.height = "auto";

        // 创建一个链接元素用于下载
        let link = document.createElement("a");
        link.href = img.src;
        link.download = "screenshot.png"; // 设置下载文件名
        link.click(); // 模拟点击触发下载
      });
      try {
        const element = document.getElementById(id);
        const screenshot = await html2canvas(element);
        this.screenshot = screenshot.toDataURL("image/png");
      } catch (error) {
        console.error("截图失败:", error);
      }
    },
  },
};
</script>
  <style lang="scss">
.title {
  margin: 0px auto 30px auto;
  text-align: center;
  color: #707070;
}
.el-table .yj-row {
  background: #f56c6c;
}

.el-table .zj-row {
  background: #f0f9eb;
}
</style>
  